namespace QMVnSoft.Opening
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.Controls.ChildGridControls;
    using QMVnSoft.Properties;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormCDZ3ELister : FormControl
    {
        private ToolStripButton btnClose;
        private ToolStripButton btnDone;
        private ToolStripButton btnFind;
        private ToolStripButton btnPreview;
        private ToolStripButton btnPrint;
        private ToolStripButton btnRefresh;
        private ToolStripButton btnSave;
        private ToolStripButton btnSum;
        private DataGridViewTextBoxColumn Column1;
        private NumericColumn Column10;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column3;
        private DataGridViewTextBoxColumn Column4;
        private DataGridViewTextBoxColumn Column5;
        private DataGridViewTextBoxColumn Column6;
        private NumericColumn Column7;
        private NumericColumn Column8;
        private NumericColumn Column9;
        private IContainer components;
        private GridControl grdBrowser;
        private DataRow oCurrentRow;
        private DataSet oLocalDataSet;
        private object oUserDefined1;
        private object oUserDefined2;
        private ToolStripSeparator tbrSep01;
        private ToolStripSeparator tbrSep02;
        private ToolStripSeparator tbrSep04;
        private ToolStripSeparator tbrSep05;
        private ToolStripSeparator tbrSep06;
        private ToolStripSeparator tbrSep07;
        private ToolStrip Toolbars;
        private TbarSearch txtSearch;

        public FormCDZ3ELister()
        {
            this.InitializeComponent();
            this.grdBrowser.AutoGenerateColumns = false;
            this.OnFillData();
        }

        public FormCDZ3ELister(object poUserDefined1, object poUserDefined2)
        {
            this.InitializeComponent();
            this.grdBrowser.AutoGenerateColumns = false;
            this.UserDefined1 = poUserDefined1;
            this.UserDefined2 = poUserDefined2;
            string headerText = "";
            foreach (DataGridViewColumn column in this.grdBrowser.Columns)
            {
                headerText = column.HeaderText;
                column.HeaderCell = new HeaderControl();
                column.HeaderText = headerText;
            }
            this.OnFillData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            this.OnDone();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.OnRefresh();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.OnSave();
        }

        private void btnSum_Click(object sender, EventArgs e)
        {
        }

        private void Calculate()
        {
            DataRow row = ((DataRowView) this.grdBrowser.CurrentRow.DataBoundItem).Row;
            this.Calculate(row);
        }

        private void Calculate(DataRow po_Row)
        {
            if (((decimal) po_Row["Ton_Cuoi"]) == ((decimal) po_Row["SL_Xuat"]))
            {
                po_Row["Du_Cuoi"] = po_Row["Tien_Xuat"];
            }
            else
            {
                po_Row["Du_Cuoi"] = decimal.Round((((decimal) po_Row["Ton_Cuoi"]) * ((decimal) po_Row["Tien_Xuat"])) / ((decimal) po_Row["SL_Xuat"]), AppInstance.LCAmountDecimals);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void grdBrowser_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 8)
            {
                this.Calculate();
            }
        }

        private void grdBrowser_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 8)
            {
                DataRow row = ((DataRowView) this.grdBrowser.CurrentRow.DataBoundItem).Row;
                if (this.grdBrowser.CurrentCell.IsInEditMode)
                {
                    row["Ton_Cuoi"] = Convert.ToDecimal(string.IsNullOrEmpty(e.FormattedValue.ToString()) ? "0" : e.FormattedValue);
                }
                if (((decimal) row["Ton_Cuoi"]) > ((decimal) row["SL_Xuat"]))
                {
                    e.Cancel = true;
                    (this.grdBrowser.CurrentCell as NumericCell).CheckMovable = false;
                    MessageBox.Show("Số lượng vật tư chưa sử dụng không \nđược lớn hơn số lượng xuất kho!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    e.Cancel = false;
                    (this.grdBrowser.CurrentCell as NumericCell).CheckMovable = true;
                }
            }
        }

        private void InitializeComponent()
        {
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
            this.Toolbars = new System.Windows.Forms.ToolStrip();
            this.btnRefresh = new System.Windows.Forms.ToolStripButton();
            this.tbrSep05 = new System.Windows.Forms.ToolStripSeparator();
            this.btnDone = new System.Windows.Forms.ToolStripButton();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.tbrSep01 = new System.Windows.Forms.ToolStripSeparator();
            this.txtSearch = new QMVnSoft.Controls.TbarSearch();
            this.tbrSep07 = new System.Windows.Forms.ToolStripSeparator();
            this.btnFind = new System.Windows.Forms.ToolStripButton();
            this.tbrSep02 = new System.Windows.Forms.ToolStripSeparator();
            this.btnSum = new System.Windows.Forms.ToolStripButton();
            this.tbrSep04 = new System.Windows.Forms.ToolStripSeparator();
            this.btnPreview = new System.Windows.Forms.ToolStripButton();
            this.btnPrint = new System.Windows.Forms.ToolStripButton();
            this.tbrSep06 = new System.Windows.Forms.ToolStripSeparator();
            this.btnClose = new System.Windows.Forms.ToolStripButton();
            this.grdBrowser = new QMVnSoft.Controls.GridControl();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column7 = new QMVnSoft.Controls.ChildGridControls.NumericColumn();
            this.Column8 = new QMVnSoft.Controls.ChildGridControls.NumericColumn();
            this.Column9 = new QMVnSoft.Controls.ChildGridControls.NumericColumn();
            this.Column10 = new QMVnSoft.Controls.ChildGridControls.NumericColumn();
            this.Toolbars.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdBrowser)).BeginInit();
            this.SuspendLayout();
            // 
            // Toolbars
            // 
            this.Toolbars.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnRefresh,
            this.tbrSep05,
            this.btnDone,
            this.btnSave,
            this.tbrSep01,
            this.txtSearch,
            this.tbrSep07,
            this.btnFind,
            this.tbrSep02,
            this.btnSum,
            this.tbrSep04,
            this.btnPreview,
            this.btnPrint,
            this.tbrSep06,
            this.btnClose});
            this.Toolbars.Location = new System.Drawing.Point(0, 0);
            this.Toolbars.Name = "Toolbars";
            this.Toolbars.Size = new System.Drawing.Size(847, 25);
            this.Toolbars.TabIndex = 0;
            this.Toolbars.Text = "Toolbars";
            // 
            // btnRefresh
            // 
            this.btnRefresh.Image = global::QMVnSoft.Properties.Resources.REFRESH;
            this.btnRefresh.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(66, 22);
            this.btnRefresh.Text = "Refresh";
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // tbrSep05
            // 
            this.tbrSep05.Name = "tbrSep05";
            this.tbrSep05.Size = new System.Drawing.Size(6, 25);
            // 
            // btnDone
            // 
            this.btnDone.Image = global::QMVnSoft.Properties.Resources.DONE;
            this.btnDone.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnDone.Name = "btnDone";
            this.btnDone.Size = new System.Drawing.Size(23, 22);
            this.btnDone.ToolTipText = "Xác định giá trị (F10)";
            this.btnDone.Click += new System.EventHandler(this.btnDone_Click);
            // 
            // btnSave
            // 
            this.btnSave.Image = global::QMVnSoft.Properties.Resources.SAVE1;
            this.btnSave.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(47, 22);
            this.btnSave.Text = "Lưu";
            this.btnSave.ToolTipText = "Lưu (Ctrl+Enter)";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // tbrSep01
            // 
            this.tbrSep01.Name = "tbrSep01";
            this.tbrSep01.Size = new System.Drawing.Size(6, 25);
            // 
            // txtSearch
            // 
            this.txtSearch.Name = "txtSearch";
            this.txtSearch.Size = new System.Drawing.Size(100, 25);
            this.txtSearch.Text = "Tìm nhanh...";
            this.txtSearch.ToolTipText = "F4";
            // 
            // tbrSep07
            // 
            this.tbrSep07.Name = "tbrSep07";
            this.tbrSep07.Size = new System.Drawing.Size(6, 25);
            // 
            // btnFind
            // 
            this.btnFind.Image = global::QMVnSoft.Properties.Resources.FIND;
            this.btnFind.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnFind.Name = "btnFind";
            this.btnFind.Size = new System.Drawing.Size(48, 22);
            this.btnFind.Text = "Tìm";
            this.btnFind.ToolTipText = "Tìm kiếm";
            this.btnFind.Click += new System.EventHandler(this.btnFind_Click);
            // 
            // tbrSep02
            // 
            this.tbrSep02.Name = "tbrSep02";
            this.tbrSep02.Size = new System.Drawing.Size(6, 25);
            // 
            // btnSum
            // 
            this.btnSum.Image = global::QMVnSoft.Properties.Resources.SUM;
            this.btnSum.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSum.Name = "btnSum";
            this.btnSum.Size = new System.Drawing.Size(55, 22);
            this.btnSum.Text = "Tổng";
            this.btnSum.ToolTipText = "Tổng cộng";
            this.btnSum.Click += new System.EventHandler(this.btnSum_Click);
            // 
            // tbrSep04
            // 
            this.tbrSep04.Name = "tbrSep04";
            this.tbrSep04.Size = new System.Drawing.Size(6, 25);
            // 
            // btnPreview
            // 
            this.btnPreview.Image = global::QMVnSoft.Properties.Resources.PREVIEW1;
            this.btnPreview.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPreview.Name = "btnPreview";
            this.btnPreview.Size = new System.Drawing.Size(51, 22);
            this.btnPreview.Text = "Xem";
            this.btnPreview.ToolTipText = "Xem trước khi in (Ctrl+F7)";
            this.btnPreview.Click += new System.EventHandler(this.btnPreview_Click);
            // 
            // btnPrint
            // 
            this.btnPrint.Image = global::QMVnSoft.Properties.Resources.PRINT1;
            this.btnPrint.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new System.Drawing.Size(37, 22);
            this.btnPrint.Text = "In";
            this.btnPrint.ToolTipText = "In (F7)";
            this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
            // 
            // tbrSep06
            // 
            this.tbrSep06.Name = "tbrSep06";
            this.tbrSep06.Size = new System.Drawing.Size(6, 25);
            this.tbrSep06.Visible = false;
            // 
            // btnClose
            // 
            this.btnClose.Image = global::QMVnSoft.Properties.Resources.CLOSE;
            this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(58, 22);
            this.btnClose.Text = "Thoát";
            this.btnClose.ToolTipText = "Thoát (Esc)";
            this.btnClose.Visible = false;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // grdBrowser
            // 
            this.grdBrowser.AllowUserToAddRows = false;
            this.grdBrowser.AllowUserToDeleteRows = false;
            this.grdBrowser.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells;
            this.grdBrowser.CheckColor = false;
            this.grdBrowser.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdBrowser.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2,
            this.Column3,
            this.Column4,
            this.Column5,
            this.Column6,
            this.Column7,
            this.Column8,
            this.Column9,
            this.Column10});
            this.grdBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.grdBrowser.Font = new System.Drawing.Font("Arial", 8.25F);
            this.grdBrowser.Location = new System.Drawing.Point(0, 25);
            this.grdBrowser.Name = "grdBrowser";
            this.grdBrowser.Size = new System.Drawing.Size(847, 241);
            this.grdBrowser.TabIndex = 1;
            this.grdBrowser.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.grdBrowser_CellValidating);
            this.grdBrowser.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.grdBrowser_CellEndEdit);
            // 
            // Column1
            // 
            this.Column1.DataPropertyName = "Ma_Yt";
            this.Column1.HeaderText = "Yếu tố";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Width = 52;
            // 
            // Column2
            // 
            this.Column2.DataPropertyName = "Ten_Yt";
            this.Column2.HeaderText = "Tên yếu tố";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Width = 69;
            // 
            // Column3
            // 
            this.Column3.DataPropertyName = "Ma_Dm_Sp";
            this.Column3.HeaderText = "Mã định mức";
            this.Column3.Name = "Column3";
            this.Column3.ReadOnly = true;
            this.Column3.Width = 86;
            // 
            // Column4
            // 
            this.Column4.DataPropertyName = "Ten_Dm_Sp";
            this.Column4.HeaderText = "Tên định mức";
            this.Column4.Name = "Column4";
            this.Column4.ReadOnly = true;
            this.Column4.Width = 89;
            // 
            // Column5
            // 
            this.Column5.DataPropertyName = "Dvt";
            this.Column5.HeaderText = "Đơn vị tính";
            this.Column5.Name = "Column5";
            this.Column5.ReadOnly = true;
            this.Column5.Width = 62;
            // 
            // Column6
            // 
            this.Column6.DataPropertyName = "Ma_Sp";
            this.Column6.HeaderText = "Sản phẩm";
            this.Column6.Name = "Column6";
            this.Column6.ReadOnly = true;
            this.Column6.Width = 74;
            // 
            // Column7
            // 
            this.Column7.DataPropertyName = "SL_Xuat";
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            this.Column7.DefaultCellStyle = dataGridViewCellStyle1;
            this.Column7.HeaderText = "Số lượng xuất";
            this.Column7.Name = "Column7";
            this.Column7.ReadOnly = true;
            this.Column7.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.Column7.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column7.UserTextNumericFormat  = QMVnSoft.AppInstance.TextNumericFormat.Quantity;
            this.Column7.Width = 92;
            // 
            // Column8
            // 
            this.Column8.DataPropertyName = "Tien_Xuat";
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            this.Column8.DefaultCellStyle = dataGridViewCellStyle2;
            this.Column8.HeaderText = "Giá trị xuất";
            this.Column8.Name = "Column8";
            this.Column8.ReadOnly = true;
            this.Column8.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column8.UserTextNumericFormat  = QMVnSoft.AppInstance.TextNumericFormat.LocalAmount;
            this.Column8.Width = 78;
            // 
            // Column9
            // 
            this.Column9.DataPropertyName = "Ton_Cuoi";
            dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            this.Column9.DefaultCellStyle = dataGridViewCellStyle3;
            this.Column9.HeaderText = "Số lượng chưa sử dụng";
            this.Column9.Name = "Column9";
            this.Column9.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column9.UserTextNumericFormat  = QMVnSoft.AppInstance.TextNumericFormat.Quantity;
            this.Column9.Width = 113;
            this.Column9.ZeroIsValid = true;
            // 
            // Column10
            // 
            this.Column10.DataPropertyName = "Du_Cuoi";
            dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            this.Column10.DefaultCellStyle = dataGridViewCellStyle4;
            this.Column10.HeaderText = "Giá trị chưa sử dụng";
            this.Column10.Name = "Column10";
            this.Column10.ReadOnly = true;
            this.Column10.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column10.UserTextNumericFormat  = QMVnSoft.AppInstance.TextNumericFormat.LocalAmount;
            this.Column10.Width = 98;
            // 
            // FormCDZ3ELister
            // 
            this.ClientSize = new System.Drawing.Size(847, 266);
            this.Controls.Add(this.grdBrowser);
            this.Controls.Add(this.Toolbars);
            this.Name = "FormCDZ3ELister";
            this.Text = "Vật tư chưa sử dụng cuối kỳ";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Toolbars.ResumeLayout(false);
            this.Toolbars.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdBrowser)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void OnDone()
        {
            foreach (DataRow row in this.LocalTable.Rows)
            {
                this.Calculate(row);
            }
        }

        public virtual void OnFillData()
        {
            this.OnRefresh();
        }

        public virtual void OnFilter()
        {
        }

        public virtual void OnPreview()
        {
        }

        public virtual void OnPrint()
        {
        }

        public virtual void OnRefresh()
        {
            this.LocalDataSet = new CDSLCKDataAccess().GetDataSet(AppInstance.Subsidiary, this.UserDefined1, this.UserDefined2);
            this.LocalGridBrowser.DataSource = new BindingSource(this.LocalDataSet, this.LocalTable.TableName);
        }

        private void OnSave()
        {
            if ((((DateTime) this.UserDefined2) < AppInstance.OpeningDateOfSubEnterprice) || (((DateTime) this.UserDefined2) <= AppInstance.EndingDateOfSubEnterprice))
            {
                MessageBox.Show("Đã khóa dữ liệu!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                new CDSLCKDataAccess().Save(AppInstance.Subsidiary, this.UserDefined2, this.LocalDataSet);
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F3:
                    this.OnSave();
                    return true;

                case Keys.F4:
                    this.txtSearch.Focus();
                    return true;

                case Keys.F7:
                    this.OnPrint();
                    return true;

                case Keys.Escape:
                    base.Close();
                    return true;

                case Keys.F10:
                    this.OnDone();
                    return true;

                case (Keys.Control | Keys.Return):
                    this.OnSave();
                    base.Close();
                    return true;

                case (Keys.Control | Keys.F7):
                    this.OnPreview();
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        [Category("Browser properties"), Browsable(false)]
        public DataRow LocalCurrentRow
        {
            get
            {
                try
                {
                    if ((this.LocalGridBrowser.CurrentRow == null) || (this.LocalGridBrowser.CurrentRow.DataBoundItem == null))
                    {
                        this.oCurrentRow = null;
                    }
                    else
                    {
                        this.oCurrentRow = ((DataRowView) this.LocalGridBrowser.CurrentRow.DataBoundItem).Row;
                    }
                }
                catch
                {
                    this.oCurrentRow = null;
                }
                return this.oCurrentRow;
            }
            set
            {
                this.oCurrentRow = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public DataSet LocalDataSet
        {
            get
            {
                return this.oLocalDataSet;
            }
            set
            {
                this.oLocalDataSet = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public GridControl LocalGridBrowser
        {
            get
            {
                return this.grdBrowser;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public DataTable LocalTable
        {
            get
            {
                if ((this.LocalDataSet != null) && (this.LocalDataSet.Tables.Count != 0))
                {
                    return this.LocalDataSet.Tables[0];
                }
                return null;
            }
        }

        public object UserDefined1
        {
            get
            {
                return this.oUserDefined1;
            }
            set
            {
                this.oUserDefined1 = value;
            }
        }

        public object UserDefined2
        {
            get
            {
                return this.oUserDefined2;
            }
            set
            {
                this.oUserDefined2 = value;
            }
        }
    }
}

